package huanyuxingcheng.nonameinstallhelper;

import android.os.Environment;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class NonameUtils {

    public interface Filter<T>{
        boolean filter(T t);
    }

    public static boolean qqDownloadDirectoryExist(){
        File file = getQQDownloadDirectory();
        return file.exists() && file.isDirectory();
    }

    public static ArrayList<File> getSubDirectories(File path){
        File[] files = path.listFiles();
        if(files == null){
            return new ArrayList<>();
        }
        ArrayList<File> arrayList = new ArrayList<>();
        for(File file:files){
            if(file.exists() && file.isDirectory()){
                arrayList.add(file);
            }
        }
        return arrayList;
    }

    public static ArrayList<File> fileFilter(File path, Filter<File> fileFilter){
        File[] files = path.listFiles();
        if(files == null){
            return new ArrayList<>();
        }
        ArrayList<File> arrayList = new ArrayList<>();
        for(File file:files){
            if(fileFilter.filter(file)){
                arrayList.add(file);
            }
        }
        Collections.sort(arrayList, new Comparator<File>() {
            @Override
            public int compare(File file, File t1) {
                long a = file.lastModified();
                long b = t1.lastModified();
                return Long.compare(b,a);
            }
        });
        return arrayList;
    }

    public static ArrayList<File> fileFilter(File path, String keyword,String ext){
        File[] files = path.listFiles();
        if(files == null){
            return new ArrayList<>();
        }
        ArrayList<File> arrayList = new ArrayList<>();
        for(File file:files){
            if(file.getName().contains(keyword) && file.getName().endsWith(ext) && file.isFile()){
                arrayList.add(file);
            }
        }
        Collections.sort(arrayList, new Comparator<File>() {
            @Override
            public int compare(File file, File t1) {
                long a = file.lastModified();
                long b = t1.lastModified();
                return Long.compare(b,a);
            }
        });
        return arrayList;
    }

    public static File getQQDownloadDirectory(){
        File file = Environment.getExternalStorageDirectory();
        return new File(file,"Android/data/com.tencent.mobileqq/Tencent/QQfile_recv");
    }

    public static File getNonameDirectoryA(){
        File file = Environment.getExternalStorageDirectory();
        return new File(file,"Android/data/yuri.nakamura.noname");
    }

    public static File getNonameDirectoryB(){
        File file = Environment.getExternalStorageDirectory();
        return new File(file,"Android/data/yuri.nakamura.noname_android");
    }

    public static File getNonameExtensionDirectory(String extensionName){
        return new File(getNonameDirectory(),"extension/"+extensionName);
    }

    public static boolean hasExtension(String extension){
        return getNonameExtensionDirectory(extension).exists();
    }

    public static File getNonameDecadeCardDirectory(){
        return new File(getNonameExtensionDirectory("十周年UI"),"image/card");
    }

    public static File getNonameSkinDirectory(){
        return new File(getNonameDirectory(),"image/skin");
    }

    public static File getNonameAudioDirectory(){
        return new File(getNonameDirectory(),"audio");
    }

    public static File getNonameImageDirectory(){
        return new File(getNonameDirectory(),"image");
    }

    public static boolean nonameExtensionExist(String extensionName){
        return getNonameExtensionDirectory(extensionName).exists();
    }

    public static boolean nonameDirectoryExist(){
        File file = getNonameDirectory();
        return file.exists() && file.isDirectory();
    }

    public static File getNonameDirectory(){
        File file = getNonameDirectoryB();
        if(file.exists()){
            return file;
        }
        return getNonameDirectoryA();
    }

}
